module.exports = class extends think.Model {
    /**
     * 获取商品的product
     * @param goodsId
     * @returns {Promise.<*>}
     */
    async getProductList(goodsId) {
        const goods = await this.model('product').where({goods_id: goodsId,is_delete:0}).select();
        return goods;
    }

    /**
     * 获取商品的规格信息
     * @param goodsId
     * @returns {Promise.<Array>}
     */
    async getSpecificationList(goodsId) {
        // 根据sku商品信息，查找规格值列表
        let info = await this.model('goods_specification').where({goods_id:goodsId,is_delete:0}).select();
        for(const item of info){
            let product = await this.model('product').where({
                goods_specification_ids:item.id,
                is_delete:0
            }).find();
            item.goods_number = product.goods_number;
        }
        let spec_id = info[0].specification_id;
        let specification = await this.model('specification').where({id:spec_id}).find();
        let name = specification.name;
        let data = {
            specification_id:spec_id,
            name:name,
            valueList:info
        }
        return data;
    }


    /**
     * 获取商品的积分
     * @param goodsId
     * @returns {Promise.<Array>}
     */
    async getIntegral(goodsId) {
        // 根据sku商品信息，查找规格值列表
        let info1 = await this.model('goods_integral').where({goods_id:goodsId,is_delete:0}).order('req_points').find();
        let info2 = await this.model('goods_integral').where({goods_id:goodsId,is_delete:0}).order('req_points desc').find();
        if(think.isEmpty(info1)) return 0;
        if(info1.req_points!=info2.req_points){
            return info1.req_points+'~'+info2.req_points;
        }
        return info1.req_points;
    }

    /**
     * 获取商品的规格积分信息
     * @param goodsId
     * @returns {Promise.<Array>}
     */
    async getIntegralList(goodsId) {
        let spec_ids= await this.model('goods_integral').where({goods_id:goodsId,is_delete:0}).getField('distinct spec_id');
        // 根据sku商品信息，查找规格值列表
        let info = await this.model('goods_specification').join({ table: 'goods_integral',  join: 'left',as:'gi',on: ['id', 'spec_id'] })
        .where({'hiolabs_goods_specification.id':['in',spec_ids],'hiolabs_goods_specification.is_delete':0}).select();
        for(const item of info){
            let product = await this.model('product').where({
                goods_specification_ids:item.spec_id,
                is_delete:0
            }).find();
            item.goods_number = product.goods_number;
        }
        let spec_id = info[0].specification_id;
        let specification = await this.model('specification').where({id:spec_id}).find();
        let name = specification.name;
        let data = {
            specification_id:spec_id,
            name:name,
            valueList:info
        }
        return data;
    }

     /**
     * 获取商品的product
     * @param goodsId
     * @returns {Promise.<*>}
     */
    async getProductIntegral(goodsId) {
        const goods = await this.model('product').join({ table: 'goods_integral',  join: 'right',as:'gi',on: ['goods_specification_ids', 'spec_id'] })
        .where({'gi.goods_id': goodsId,'hiolabs_product.is_delete':0,'gi.is_delete':0})
        .field('hiolabs_product.*,gi.spec_id, gi.get_points, gi.req_points, gi.tips, gi.total, gi.limit, gi.is_delete as g_delete, gi.sort_order, gi.gettype, gi.gi_desc, gi.spare_field, gi.begin_time, gi.end_time, gi.create_time, gi.update_time').select();
        return goods;
    }
};
